//
//  FilmVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "FilmVC.h"
#import "FilmTableVC.h"

@interface FilmVC ()

@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UIButton *currentButton;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UIImageView *picView;
@property (nonatomic, strong) UIPageControl *page;

@end

@implementation FilmVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    // bView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:bView];
    
//    for (int i = 0; i < 3; i++) {
//        UIButton *button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
//        [button setTitle:array[i] forState:(UIControlStateNormal)];
//        // button.backgroundColor = [UIColor greenColor];
//        [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
//        button.frame = CGRectMake(2 * (kScreenWidth - 270) / 3 + 90 *i, 14, 70, 30);
//        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//        button.tag = 1 + i;
//        [bView addSubview:button];
//    }
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
    backButton.frame = CGRectMake(15, 26, 30, 30);
    // [backButton setTitle:@"返回" forState:(UIControlStateNormal)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"houtuiyibu"] forState:(UIControlStateNormal)];
    // [backButton setTitleColor:[UIColor darkGrayColor] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(actionBack:) forControlEvents:(UIControlEventTouchUpInside)];
    [bView addSubview:backButton];
    
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 40) / 2, 26, 70, 30)];
    self.titleLable.text = @"新影讯";
    self.titleLable.font = [UIFont systemFontOfSize:16];
    self.titleLable.textAlignment = NSTextAlignmentLeft;
    self.titleLable.textColor = [UIColor blackColor];
    [bView addSubview:self.titleLable];
    
    self.picView = [[UIImageView alloc] initWithFrame:CGRectMake(self.titleLable.left - 22, 30, 20, 20)];
    self.picView.image = [UIImage imageNamed:@"lan_kong 2"];
    [bView addSubview:self.picView];
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.picView.left, self.titleLable.bottom, 70, 4)];
    self.page.numberOfPages = 3;
    _page.pageIndicatorTintColor = [UIColor lightGrayColor];
    _page.currentPageIndicatorTintColor = [UIColor blackColor];
    self.page.currentPage = 0;
    // self.page.backgroundColor = [UIColor blackColor];
    // self.page.tintColor = [UIColor blackColor];
    [bView addSubview:self.page];
    
    
    
//    self.currentButton = bView.subviews[0];
//    [self.currentButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    self.whiteView = [[UIView alloc] initWithFrame:CGRectMake(2 *(kScreenWidth - 270) / 3, 44, 70, 4)];
//    _whiteView.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:_whiteView];
    
    self.bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 66, kScreenWidth, kScreenHeight)];
    self.bgScrollView.contentSize = CGSizeMake(kScreenWidth * 3, kScreenHeight);
    self.bgScrollView.pagingEnabled = YES;
    self.bgScrollView.bounces = NO;
    self.bgScrollView.delegate = self;
    [self.view addSubview:self.bgScrollView];
    
    // NSArray *arrayUrl = @[KNewsUrl,KTrailerUrl,KTopListURL];
//    for (int i = 0; i < 3; i++) {
//        FilmTableVC *FVC = [[FilmTableVC alloc] init];
//        FVC.tableView.frame = CGRectMake(kScreenWidth * i, 50, kScreenWidth, kScreenHeight - 50);
//        [FVC urlConnectionWithURLString:arrayUrl[i]];
//        [self addChildViewController:FVC];
//        [self.bgScrollView addSubview:FVC.tableView];
//    }
    
    FilmTableVC *fVC = [[FilmTableVC alloc] init];
    fVC.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 50);
    [fVC urlConnectionWithURLString:KNewsUrl];
    [self addChildViewController:fVC];
    [self.bgScrollView addSubview:fVC.tableView];
    
    FilmTableVC *fVCTr = [[FilmTableVC alloc] init];
    fVCTr.tableView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 50);
    [fVCTr urlConnectionWithURLString:KTrailerUrl];
    [self addChildViewController:fVCTr];
    [self.bgScrollView addSubview:fVCTr.tableView];
    
    
    FilmTableVC *fVCTop = [[FilmTableVC alloc] init];
    fVCTop.tableView.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, kScreenHeight - 50);
    [fVCTop urlConnectionWithURLString:KTopListURL];
    [self addChildViewController:fVCTop];
    [self.bgScrollView addSubview:fVCTop.tableView];
    
    
}

//  返回button的方法
-(void)actionBack:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}




//- (void)actionButton:(UIButton *)button
//{
//   //  [button setTitleColor:[UIColor blueColor] forState:(UIControlStateSelected)];
//    
//    self.whiteView.frame = CGRectMake(2 * (kScreenWidth - 270) / 3 + 90 * (button.tag - 1) , 44, 70, 4);
//    if (self.currentButton.tag != button.tag) {
//        [self.currentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        self.currentButton.titleLabel.font = [UIFont systemFontOfSize:15];
//        [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
//        self.currentButton = button;
//        
//        
//    }
//
//    self.bgScrollView.contentOffset = CGPointMake(kScreenWidth * (button.tag - 1), 50);
//    
//}


#pragma mark -- 实现ScrollView的代理,顶部view跟随
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//
//    self.whiteView.frame = CGRectMake((scrollView.contentOffset.x / kScreenWidth) *90 + 2 * (kScreenWidth - 270) / 3, 44, 70, 4);
//    
//    
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    if (self.currentButton.tag != scrollView.contentOffset.x / kScreenWidth + 1) {
//        [self.currentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        self.currentButton.titleLabel.font = [UIFont systemFontOfSize:15];
//        UIButton *button = (UIButton *)[self.view viewWithTag:scrollView.contentOffset.x / kScreenWidth + 1];
//        [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
//        self.currentButton = button;
//        
//
//    }
    UIColor *blueColor = [UIColor blackColor];
    UIColor *redColor = [UIColor blackColor];
    UIColor *greenColor = [UIColor blackColor];
    NSArray *colorArray = @[blueColor,redColor,greenColor];
    NSArray *array = @[@"新影讯",@"预告片",@"排行榜"];
    NSArray *imageArray = @[@"lan_kong 2",@"hong_kong 2",@"lv_kong 2"];
    NSInteger count = scrollView.contentOffset.x / kScreenWidth;
    self.titleLable.text = array[count];
    self.titleLable.textColor = colorArray[count];
    self.picView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArray[count]]];
    self.page.currentPage = count;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
